/**
 * 使用面向对象的语言实现一个计算器控制台程序，要求输入两个数和运算符，得到结果
 *
 * 工厂方法模式
 */

export abstract class Operation {
  private numberA!: number;
  private numberB!: number;

  get NumberA(): number {
    return this.numberA;
  }

  set NumberA(val: number) {
    this.numberA = val;
  }

  get NumberB(): number {
    return this.numberB;
  }

  set NumberB(val: number) {
    this.numberB = val;
  }

  public GetResult(): number | null {
    return null;
  }
}

class OperationAdd extends Operation {
  public GetResult(): number | null {
    return this.NumberA + this.NumberB;
  }
}

class OperationSub extends Operation {
  public GetResult(): number | null {
    return this.NumberA - this.NumberB;
  }
}

class OperationMul extends Operation {
  public GetResult(): number | null {
    return this.NumberA * this.NumberB;
  }
}

class OperationDiv extends Operation {
  public GetResult(): number | null {
    if (this.NumberB === 0) {
      console.log("被除数为0");
      return null;
    }
    return this.NumberA / this.NumberB;
  }
}

export interface IFactory {
  CreateOperation(): Operation;
}

export class AddFactory implements IFactory {
  public CreateOperation(): Operation {
    return new OperationAdd();
  }
}
export class SubFactory implements IFactory {
  public CreateOperation(): Operation {
    return new OperationSub();
  }
}
export class MulFactory implements IFactory {
  public CreateOperation(): Operation {
    return new OperationMul();
  }
}
export class DivFactory implements IFactory {
  public CreateOperation(): Operation {
    return new OperationDiv();
  }
}
